<template>
	<view>
		<view v-for="(item,index) in datalist" :key="item.leave_id" @click="toupda(item)">
			<uni-card :title="item.name+'的请假'" :extra=" timeDifference(item.apply_time)">
				<view class="uni-body">
					<view class="" style="display: flex;justify-content: space-between;">
						<span>请假日期:&ensp;{{item.star_time}}&ensp;</span>
						<span>至:&ensp;{{item.end_time}}</span>
					</view>		
					<view class="" style="display: flex;justify-content: space-between;">
						<span>请假天数:&ensp;{{item.days}}&ensp;</span>
						<span>申请时间:&ensp;{{item.apply_time}}</span>						
					</view>
					<view class="">
						<span>请假理由:&ensp;{{item.reason}}</span>
					</view>
					<view class="" style="display: flex;justify-content: space-between;">
						<span>申请部门:&ensp;{{item.department}}&ensp;</span>
						<span>申请人:&ensp;{{item.applicant}}</span>						
					</view>
				</view>
			</uni-card>
		</view>
			
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				datalist: [{
						"name": '张三',
						"star_time": "2025-06-10 09:00",
						"end_time": "2025-06-12 18:00",
						"days": 3,
						"apply_time": "2025-06-09 14:30",
						"reason": "家庭紧急事务",
						"department": "人力资源部门",
						"applicant": "张三",
						"leave_id": "LJ2025061001"
					},
					{
						"name": '李四',
						"star_time:": "2025-06-15 09:00",
						"end_time": "2025-06-17 18:00",
						"days": 3,
						"apply_time": "2025-06-09 14:30",
						"reason": "健康原因",
						"department": "技术部",
						"applicant": "李四",
						"leave_id": "LJ2025061501"
					}
				]

			}
		},
		onShow(e) {		
			const upda =uni.getStorageSync("updata")
			
			this.datalist = this.datalist.map(item => {
				if (item.leave_id === upda.leave_id) {
					return upda; // 替换当前项
				}
				return item; // 保留原项
			});
		},
		onHide() {
			uni.clearStorage()
		},
		methods: {
			
			// 计算申请时间
			 timeDifference(applyTimeString) {
			      const applyTime = new Date(applyTimeString.replace(' ', 'T'));
			      const currentTime = new Date();
			      const diffInMs = currentTime - applyTime;
			      const diffInSeconds = Math.floor(diffInMs / 1000);
			      const diffInMinutes = Math.floor(diffInSeconds / 60);
			      const diffInHours = Math.floor(diffInMinutes / 60);
			      const diffInDays = Math.floor(diffInHours / 24);
			      
			      if (diffInDays > 0) {
			        return `${diffInDays}天${diffInHours % 24}小时`;
			      } else if (diffInHours > 0) {
			        return `${diffInHours}小时${diffInMinutes % 60}分钟`;
			      } else {
			        return `${diffInMinutes}分钟`;
			      }
			    },
			toupda(e){
				uni.navigateTo({
					url: './AskForUpda?data=' + JSON.stringify(e)
				});
			}
		}
	}
</script>

<style>

</style>